﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL.RF.Warehouse;
using Model.RF.Warehouse;

namespace DAL.RF.Warehouse
{
    public class StockOut:BaseRepository_DT<StockOutInfo>,IStockOut
    {
        #region 静态变量部分
        private const string PARM_STOCKOUT_PACKAGEPRODUCTSN = "@PackageProductSN";
        private const string PARM_STOCKOUT_PRODUCTTYPEID = "@ProductTypeID";
        private const string PARM_STOCKOUT_USERID = "@UserID";
        private const string PARM_STOCKOUT_ISCHECKPACKAGESTATUS = "@IsCheckPackageStatus";

        private const string PARM_STOCKOUT_DT = "@ProductSNItemdt";
        #endregion


        public string StockOut_InvalidPackageSNs(StockOutInfo si, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_STOCKOUT_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(si, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "StockOut_IsPackageSNs";
                string str = Convert.ToString(cmd.ExecuteScalar());
                return str;
            }
        }

        public string StockOut_InvalidPackintoBoxSNs(StockOutInfo si, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_STOCKOUT_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(si, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "StockOut_IsPackintoBoxSNs";
                string str = Convert.ToString(cmd.ExecuteScalar());
                return str;
            }
        }

        public string StockOut_InvalidStockOutSNs(StockOutInfo si, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_STOCKOUT_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(si, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "StockOut_IsStockOutSNs";
                string str = Convert.ToString(cmd.ExecuteScalar());
                return str;
            }
        }

        public string StockOut_InvalidPIDSNs(StockOutInfo si, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_STOCKOUT_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(si, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "StockOut_IsvalidPID";
                string str = Convert.ToString(cmd.ExecuteScalar());
                return str;
            }
        }

        public bool StockOut_Insert(StockOutInfo si, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_STOCKOUT_DT,itemdt)};
            SqlParameter[] Parms = GetFlagParameters();
            SetFlagParameters(si, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                //try
                //{
                    cmd.CommandText = "StockOut_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                //}
                //catch
                //{
                //    return false;
                //}
            }
        }

        public bool StockOut_DeleteBatch(StockOutInfo si, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_STOCKOUT_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(si, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "StockOut_DeleteBatch";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }


        #region 数据检索部分
        public IList<StockOutInfo> LoadEntities()
        {
            return null;
        }

        /// <summary>
        /// Get an individual category based on a provided id
        /// </summary>
        /// <param name="categoryId">Category id</param>
        /// <returns>Details about the Category</returns>
        public StockOutInfo LoadEntity<T1>(T1 packageProductSN)
        {
            SqlParameter parm = GetParameter();
            parm.Value = packageProductSN;
            return ExcuteSqlToGetEntity("StockOut_selectbySN", parm);
        }

        public IList<StockOutInfo> StockOut_GetbyDT(DateTime dt1, DateTime dt2)
        {
            SqlParameter[] parm = GetDTParameters();
            parm[0].Value = dt1;
            parm[1].Value = dt2;
            return ExcuteSqlToGetEntities("StockOut_selectbyDT", parm);
        }

        public IList<StockOutInfo> StockOut_GetbyPIDDT(string productTypeID, DateTime starttime, DateTime stoptime)
        {
            SqlParameter[] parms = GetPIDDTParameters();
            parms[0].Value = productTypeID;
            parms[1].Value = starttime;
            parms[2].Value = stoptime;
            return ExcuteSqlToGetEntities("StockOut_selectbyPIDDT", parms);
        }
        #endregion


        #region 重写基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override StockOutInfo GetEntityRecord(SqlDataReader rdr)
        {
            StockOutInfo pro = new StockOutInfo() 
            { 
                PackageProductSN = rdr.GetValue(0).ToString(), 
                RecordTime = rdr.GetDateTime(1), 
                ProductTypeID = rdr.GetValue(2).ToString(), 
                UserID = rdr.GetValue(3).ToString(),
                IsCheckPackageStatus=rdr.GetBoolean(4)
            };
            return pro;
        }

        public override StockOutInfo GetEntityRecord()
        {
            StockOutInfo pro = null;
            return pro;
        }

        public override string GetInsertsp()
        {
            return null;
        }

        public override string GetUpdatesp()
        {
            return null;
        }

        public override string GetDeletesp()
        {
            return "StockOut_Delete";
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_STOCKOUT_PACKAGEPRODUCTSN, SqlDbType.NVarChar, 100);
            return Parm;
        }

        public SqlParameter[] GetPIDDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_STOCKOUT_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_STARTTIME,SqlDbType.DateTime,8),
					new SqlParameter(PARM_STOPTIME, SqlDbType.DateTime,8)
                };
            return parms;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_STOCKOUT_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_STOCKOUT_USERID, SqlDbType.NVarChar, 10)
                };
            return parms;
        }

        public override void SetParameters(StockOutInfo ad, params SqlParameter[] Parms)
        {
            Parms[0].Value = ad.ProductTypeID;
            Parms[1].Value = ad.UserID;
        }

        public SqlParameter[] GetFlagParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_STOCKOUT_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_STOCKOUT_USERID, SqlDbType.NVarChar, 10),
                    new SqlParameter(PARM_STOCKOUT_ISCHECKPACKAGESTATUS,SqlDbType.Bit,1)
                };
            return parms;
        }

        public void SetFlagParameters(StockOutInfo ad, params SqlParameter[] Parms)
        {
            Parms[0].Value = ad.ProductTypeID;
            Parms[1].Value = ad.UserID;
            Parms[2].Value = ad.IsCheckPackageStatus;
        }
        #endregion
    }
}